﻿/***************************
* CLASS GASLAYER
* CREATES A TILEMAP ASSEMBLY
* SCROLLING BY GOTOANDSTOP
***************************/

class com.gamepackage.tilemap.layer.GASLayer
	implements com.gamepackage.tilemap.TileLayer, com.gamepackage.util.Loadable
{
	private var parent: MovieClip;
	private var depth: Number;
	private var map: Array;
	private var cols: Number;
	private var rows: Number;
	private var tilesize: Number;
	private var linkageId: String;
	
	private var timeline: MovieClip;

	private var clips: Array;

	private var x: Number, y: Number;
	private var ttx: Number, tty: Number;
	
	private var jx: Number;
	private var jy: Number;
	
	private var mcl: Number;
	
	private var layerWidth: Number;
	private var layerHeight: Number;
	
	private var screenWidth: Number;
	private var screenHeight: Number;
	
	private var childCount: Number;
	private var childDepth: Number;
	private var depthSpace: Number;
	
	private var centered: Boolean = false;
	
	private var offsetX: Number;
	private var offsetY: Number;
	
	function GASLayer( parent: MovieClip, depth: Number, map: Array, cols: Number, rows: Number, tilesize: Number, linkageId: String, centered: Boolean )
	{
		this.parent = parent;
		this.depth = depth;
		this.map = map;
		this.cols = cols;
		this.rows = rows;
		this.tilesize = tilesize;
		this.linkageId = linkageId;
		this.centered = centered;
		
		init();
	}
	
	private function init( Void ): Void
	{
		//-- CREATE TIMELINE --//
		timeline = parent.createEmptyMovieClip( linkageId , depth );
		
		screenWidth = cols * tilesize;
		screenHeight = rows * tilesize;
		
		layerWidth = map[0].length * tilesize;
		layerHeight = map.length * tilesize;
		
		childCount = 0;
		depthSpace = 0;
	}
	
	function create( Void ): Void
	{
		//-- COMPUTE JUMP OFFSET --//
		jx = ( cols + 1 ) * tilesize;
		jy = ( rows + 1 ) * tilesize;
		
		offsetX = ( centered ) ? jx / 2 - tilesize / 2 : 0;
		offsetY = ( centered ) ? jy / 2 - tilesize / 2 : 0;
		
		//-- BUILD CLIPS --//
		clips = new Array;
		
		var i: Number = 0;
		var rx, ry;
		var clip: MovieClip;
		
		for( ry = 0 ; ry <= rows ; ry++ )
		{
			clips[ry] = new Array;
			
			for( rx = 0 ; rx <= cols ; rx++ )
			{
				clip = clips[ry][rx] = timeline.attachMovie(
					linkageId,
					i.toString(),
					i++,
					{
						_x: rx * tilesize - offsetX,
						_y: ry * tilesize - offsetY
					}
				);
				clip.i = clip.getDepth();
				clip.gotoAndStop( 1 );
			}
		}
		
		mcl = map[0].length + depthSpace;
		
		childDepth = ( map.length + 1 ) * mcl;
		
		ttx = tty = Number.POSITIVE_INFINITY;
	}
	
	function moveTo( x: Number, y: Number ): Void
	{
		//-- COMPARE ASSEMBLY --//
		var tx: Number = int( ( this.x = x ) / tilesize );
		var ty: Number = int( ( this.y = y ) / tilesize );
		
		var dx: Number = ttx - tx;
		var dy: Number = tty - ty;
		
		ttx = tx;
		tty = ty;
		
		//-- STORE REFERENCES LOCAL --/
		var c = clips;
		var m = map;

		//-- OTHER LOCALS --//
		var rx, ry;
		var mrow, crow;
		var clip: MovieClip;
		
		//-- MOVE TIMELINE --//
		timeline._x = -x;
		timeline._y = -y;
		
		var abs = Math.abs;
		
		if ( abs( dx ) > 1 || abs( dy ) > 1 )
		{
			//-- COMPLETE BUILD NEEDED --//
			ry = c.length;
			
			while( --ry > -1 )
			{
				mrow = m[ ry + ty ];
				crow = c[ry];
				
				rx = crow.length;
				
				while( --rx > -1 )
				{
					( clip = crow[ rx ] ).gotoAndStop( mrow[ rx + tx ] );
					clip._x = ( rx + tx ) * tilesize - offsetX;
					clip._y = ( ry + ty ) * tilesize - offsetY;
					clip.swapDepths( ( ry + ty ) * mcl + rx + tx );
					clip.i = clip.getDepth();
				}
			}
		}
		else
		{
			//-- HORIZONTAL SCROLLING --//
			if ( dx < 0 )
			{
				//-- PUSH RIGHT --//
				ry = c.length;
				
				while( --ry > -1 )
				{
					( clip = ( crow = c[ry] )[0] )._x += jx;
					
					crow.push( crow.shift() );
					
					clip.gotoAndStop( m[ ry + ty + dy ][ tx + cols ] );
					
					clip.swapDepths( ( ty + ry + dy ) * mcl + tx + cols );
					clip.i = clip.getDepth();
				}
			}
			else if ( dx > 0 )
			{
				//-- PUSH RIGHT --//
				ry = c.length;
				
				while( --ry > -1 )
				{
					crow = c[ry];
					
					crow.unshift( crow.pop() );
					
					( clip = crow[0] )._x -= jx;
					
					clip.gotoAndStop( m[ ry + ty + dy ][ tx ] );
					
					clip.swapDepths( ( ty + ry + dy ) * mcl + tx );
					clip.i = clip.getDepth();
				}
			}
			
			//-- VERTICAL SCROLLING --//
			if ( dy < 0 )
			{
				//-- PUSH DOWN --//
				c.push( crow = c.shift() );
				
				mrow = m[ ty + rows ];
				
				rx = crow.length;
				
				while( --rx > -1 )
				{
					( clip = crow[rx] )._y += jy;
					clip.gotoAndStop( mrow[ tx + rx ] );
					
					clip.swapDepths( ( ty + rows ) * mcl + tx + rx );
					clip.i = clip.getDepth();
				}
			}
			else if( dy > 0 )
			{
				//-- PUSH UP --//
				c.unshift( crow = c.pop() );
				
				mrow = m[ ty ];

				rx = crow.length;
				
				while( --rx > -1 )
				{
					( clip = crow[rx] )._y -= jy;
					clip.gotoAndStop( mrow[ tx + rx ] );
					
					clip.swapDepths( ty * mcl + tx + rx );
					clip.i = clip.getDepth();
				}
			}			
		}
	}
	
	function removeTile( rx: Number, ry: Number ): Void
	{
		var tx: Number = int( x / tilesize );
		var ty: Number = int( y / tilesize );
		
		//-- REMOVE THE MOVIECLIP --//
		clips[ry-ty][rx-tx].gotoAndStop(1);
		
		//-- CLEAR THE MAP ENTRY --//
		map[ry][rx] = 1;
	}
	
	function getTimeline( Void ): MovieClip
	{
		return timeline;
	}
	
	function getChildMovieClip( Void ): MovieClip
	{
		return timeline.createEmptyMovieClip( '_child_' + childCount++ , ++childDepth );
	}
	
	function getLayerWidth( Void ): Number
	{
		return layerWidth;
	}
	
	function getLayerHeight( Void ): Number
	{
		return layerHeight;
	}
	
	function setDepthSpace( depthSpace: Number ): Void
	{
		this.depthSpace = depthSpace;
	}
	
	function getDepthSpace( Void ): Number
	{
		return depthSpace;
	}
	
	function getRowDepth( row: Number ): Number
	{
		return ( row + 1 ) * mcl;
	}
	
	function getMap( Void ): Array
	{
		return map;
	}
	
	function getX( Void ): Number
	{
		return x;
	}
	
	function getY( Void ): Number
	{
		return y;
	}
	
	function getLoaderClip( Void ): MovieClip
	{
		return timeline;
	}
}
















